<template>
  <div class="air">
    <el-row :gutter="20">
        <el-col :span="24" class="paddCol" >
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                  <span :style="this.alarmstate == 0 ? '':'color:#ea2e2e'"><i class="el-icon-menu"></i> <span class="shebeiname">{{this.data.name}}</span></span>
              </div>
              <div class="temp_card">
                <div class="xtg401_data">
                  <div class="xtg402_flex">
                      <div class="xtg402_div" v-for="(item,index) in this.xtgControl" :key="item.index">
                        <div @click="get402click(item,index)">
                          <div :class="item.value == 0 ? 'xtg402_control xtg402_control_noclick':'xtg402_control xtg402_control_click'">
                            <svg aria-hidden="true" class="icon">
                              <use xlink:href="#icon-yijiankaiguan" />
                            </svg>
                          </div>
                          <div class="xtg402_name">{{item.name}}</div>
                        </div>
                      </div>
                    </div>
                </div>
                <div  class="xtg402_data">
                  <div class="temp_time_mode">
                    <div class="temp_peak_name">
                      <i class="el-icon-time"></i>
                      时间统计
                    </div>
                    <div class="temp_time">
                      <div class="temp_time_line">
                        <p class="temp_warning_title">首次上线：</p>
                        <p class="temp_time_year">{{ this.firstconnecttimeYear }} </p> 
                        <p class="temp_time_hour">{{ this.firstconnecttimeHour}}</p>
                      </div>
                      <div class="temp_time_line">
                        <p class="temp_warning_title">最近上线：</p>
                        <p class="temp_time_year">{{ this.connecttimeYear }} </p>
                        <p class="temp_time_hour">{{ this.connecttimeHour }}</p>
                      </div>
                      <div class="temp_time_line">
                        <p class="temp_warning_title">累计时长：</p>
                        <p class="temp_time_h"> {{this.totaltime}} </p> <span class="temp_time_s"> h</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-card>
        </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  props: ["msg"],
  data() {
    return {
      alarmstate:0,//在线离线
      timeGetData:'',//接口方法的定时器
      data: "", //当前设备数据
      dataor:1,//判断接口是否成功，1表示有返回成功，0表示失败，3之内无返回就置0表示失败
      dataCO:'',
      clickGetData:'',
      clickNum:0,
      clickState:0,
      xtgControl:[],
      //属性数据
      firstconnecttimeYear:'',//首次上线年月日
      firstconnecttimeHour:'',//首次上线时分
      connecttimeYear:'',//最近上线年月日
      connecttimeHour:'',//最近上线时分
      totaltime:0,//累计运行时间
      errorcount:0,//报警总数量
      valueerror:0,//数值报警
      connecterror:0,//链路报警
    };
  },
  mounted() {
  },
  created() {
    this.info = JSON.parse(localStorage.getItem("当前设备编号"))
    this.data = JSON.parse(localStorage.getItem("menuDataJson"))
    this.getData(JSON.parse(localStorage.getItem("当前设备编号")))
    this.getDataStart()
  },
  methods: {
    //重新定时走接口
    getDataStart(){
      this.$nextTick(() => {
          this.timeGetData = setInterval(() => {
            if(this.dataor == 0){ //接口失败提示
              this.$message({
                message: '服务器访问失败',
                type: 'error',
                duration: 1000
              });
            }
            this.getData(JSON.parse(localStorage.getItem("当前设备编号")))
            this.dataor = 0
          }, 3000); 
      })
    },
    getData(info){
      if(info.uid == null || info.uid == "null"){//清空缓存第一次进入时找不到info.uid，页面为空页面的处理
        info.uid = 0
      }
      var params = {
          "getdevinfo":[
            {
              "uid": info.uid,
              "type": info.type,
              "addr": info.addr,
              "unittype":"id-name-ready-value-alarmstate"
            }
          ],
          [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      this.$http.post(this.$baseUrl, params).then((res) => {
        this.dataor = 1// 接口成功置1
        if(res.data.getdevinfo == undefined){ //数据返回不全失败提示
          this.$message({
            message: '服务器访问失败',
            type: 'error',
            duration: 1000
          });
        }else{
          //假数据
          // res.data.getdevinfo[0].unit[6].value = 0
          this.data = res.data.getdevinfo[0];
          this.alarmstate = res.data.getdevinfo[0].alarmstate
          // 上次上线时间
          this.firstconnecttimeYear = this.formatDate(this.data.firstconnecttime*1000, 'YYYY-MM-DD')
          this.firstconnecttimeHour = this.formatDate(this.data.firstconnecttime*1000, 'H:m')
          // 最近上线时间
          this.connecttimeYear = this.formatDate(this.data.connecttime*1000, 'YYYY-MM-DD')
          this.connecttimeHour = this.formatDate(this.data.connecttime*1000, 'H:m')
          //累计运行时间 拿到的分钟转成小时
          this.totaltime = (this.data.totaltime/60).toFixed(0)
          //故障累计总条数
          this.errorcount = this.data.errorcount
          // 数据报警累计条数
          this.valueerror = this.data.valueerror
          //离线故障累计条数
          this.connecterror = this.data.connecterror
          if(this.clickNum == 0){
            var weak = []//弱电输入
            for(var i = 0; i < this.data.unit.length; i++){
              if(this.data.unit[i].ready == 1){
                weak.push(this.data.unit[i])
              }
            }
            this.xtgControl = weak
          }
          console.log(info.uid)
        }
      })
    },
    get402click(item,index){
      var params = {
        "setdevinfo":[
            {
                "uid": this.data.uid,
                "type": this.data.type,
                "addr": this.data.addr,
                "unit":[{
                  id:item.id,
                  value:item.value == 0 ? 1 :0
                }]
            }
        ],
        [localStorage.getItem("cookieName")]:localStorage.getItem("_token")
      }
      console.log(item.value)
      this.xtgControl[index].value = item.value == 0 ? 1 :0 //记录修改后的状态
      this.clickState = item.value == 0 ? 1 :0 //记录修改后的状态
      this.clickNum = 0
      clearInterval(this.clickGetData);
      this.clickGetData = null;
      this.clickGetData = setInterval(() => {
        if((this.data.unit[item.id].value != this.clickState) && (this.clickNum <= 1)){
          this.clickNum++
        }else{
          this.clickNum = 0
          var control = []//输出
          for(var i = 0; i < this.data.unit.length; i++){ // 
            if(this.data.unit[i].ready == 1){
              control.push(this.data.unit[i])
            }
          }
          this.xtgControl = control
          clearInterval(this.clickGetData);
          this.clickGetData = null;
        }
      }, 1000); 
      this.$http.post(this.$baseUrl, params).then((res) => {
          if(res.data.result != 'ok'){
            this.clickNum = 0
            clearInterval(this.clickGetData);
            this.clickGetData = null;
            this.$notify({
              title: '操作失败',
              type: 'warning',
              duration: 1500
            });
          }
      });
      
        
    }
  },
  // 清除定时器
    beforeDestroy() {
        clearInterval(this.timeGetData);
        this.timeGetData = null;
        clearInterval(this.clickGetData);
        this.clickGetData = null;
    },
};
</script>

<style scoped>
.el-card {
  background: none;
  color: #fff;
  border: 1px solid rgba(89, 129, 165, 0.4);
}
/deep/ .el-card__header {
  background: rgba(89, 129, 165, 0.2);
  padding: 16px 20px;
  border-bottom: none;
}
.box-card {
  /* overflow: hidden; */
}
.temp_card{
  height: 850px;
}
.clearfix{
  font-size: 18px;
}
.shebeiname{
  font-weight: bold;
}
.xtg402_left{
  width: 970px;
}
.xtg401_data{
  width: 900px;
  height: 350px;
  margin-top: 20px;
  border-radius: 15px;
  background: #0000002b;
}
.xtg401_title{
  height: 50px;
  padding-left: 20px;
  padding-top: 20px;
}
.xtg402_flex{
  display: flex;
  width: 900px;
  height: 210px;
  margin-top: 20px;
  flex-wrap:wrap;
  justify-content: center;
  padding-top: 100px;
  /* background: #eee; */
}
.xtg402_flex_top{
  margin-top: 45px;
}
.xtg402_div{
  width: 220px;
  height: 200px;
  /* display: flex;
  align-items: center;
  justify-content: center; */
}
.xtg401_div{
  width: 205px;
  height: 180px;
}
.xtg401_control{
  width: 60px;
  height: 60px;
  border-radius: 100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  color: #fff;
}
.xtg401_control_c{
  color: #3cdfff;
  box-shadow: 0px 0px 30px -5px #3cdfff;
}
.xtg403_control{
  width: 60px;
  height: 60px;
  border-radius: 100px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
}
.warning{
  color: red ;
  box-shadow: 0px 0px 30px -5px red;
}
.orange{
  color: orange ;
  box-shadow: 0px 0px 30px -5px orange;
}
.xtg402_control{
  width: 120px;
  height: 120px;
  margin: 0 auto;
  border: 1px solid #eeeeee14;
  background: #ffffff03;
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 60px;
  cursor: pointer;
}
.xtg402_control_click{
  color: #3cdfff;
}
.xtg402_control_click .icon{
  box-shadow: 0px 0px 30px -5px #3cdfff;
}
.xtg402_control_noclick{
  color: #898989;
}
.xtg402_control_noclick .icon{
  /* box-shadow: 0px 0px 30px -5px #eee;; */
}
.xtg402_name{
  width: 100%;
  padding: 0px 15px;
  text-align: center;
  margin-top: 10px;
  font-size: 18px;
  overflow: hidden;
}
.xtg402_data{
  display: flex;
}
/* 报警 */
.temp_warning_mode{
  width: 350px;
  height: 380px;
  padding-top: 20px;
  padding-bottom: 20px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-right: 20px;
  margin-top: 20px;
}
.temp_warning{
  margin-top: 35px;
  margin-left: 70px;
  font-size: 18px;
}
.temp_warning p{
  margin-bottom: 40px;
}
.temp_warning_title{
  color: #d4d3d3;
}
.temp_warning_data{
  font-family: electronicFont;
  font-size: 50px;
  color: #ffb50b;
}
/* 在线时间 */
.temp_time_mode{
  width: 900px;
  height: 380px;
  /* margin: 0 auto; */
  padding-top: 20px;
  border-radius: 10px;
  background: #0000002b;
  margin-bottom: 20px;
  margin-top: 20px;
}
.temp_peak_name{
  width: 100%;
  font-size: 20px;
  margin-left: 30px;
}
.temp_time{
  margin-top: 36px;
  margin-left: 230px;
}
.temp_time_line{
  margin-bottom: 58px;
  position: relative;
  top: 11px;
  display: flex;
  align-items: flex-end;
  font-size: 18px;
}
.temp_time{
  margin-right: 30px;
  color: #d4d3d3;
}
.temp_time .temp_warning_title{
  width: 130px;
  display: flex;
    align-items: flex-end;
    position: relative;
    top: -5px;
}
.temp_time_year{
  width: 210px;
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
}
.temp_time_hour{
  width: 110px;
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
}
.temp_time_h{
  font-family: electronicFont;
  font-size: 35px;
  color: #5eceff;
}
.temp_time_s{
  color: #5eceff;
  margin-left: 5px;
}
</style>